/*
 * Copyright 2012-2013 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.info.delete;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeUri;
import finbudgettable.client.ui.grid.CellGrid;

public class DeleteCell extends AbstractCell<Boolean> {
    interface Template extends SafeHtmlTemplates {
        @Template("<img src=\"{0}\" title=\"{1}\"/>")
        SafeHtml img(SafeUri url, String tooltip);

        @Template("<div class=\"{0}\">{1}</div>")
        SafeHtml div(String className, SafeHtml text);
    }

    public interface Resources extends ClientBundle {

        interface Style extends CssResource {
            @ClassName("b-delete-cell")
            String bDeleteCell();

            @ClassName("b-delete-cellbody")
            String bDeleteCellBody();

            @ClassName("b-delete-header")
            String bDeleteHeader();
        }

        @Source("DeleteCell.css")
        Style style();
    }

    interface Strings extends Messages {
    	@DefaultMessage("Отметить запись для удаления")
    	public String markToBeDeleted();
    }

    private static final Template template = GWT.create(Template.class);
    public static final Resources RESOURCES = GWT.create(Resources.class);
    private static final Strings STRINGS = GWT.create(Strings.class);

    static {
        RESOURCES.style().ensureInjected();
        CellGrid.StandardResources.Style style = CellGrid.STANDARD_RESOURCES.cellTableStyle();
        StyleInjector.inject("." + style.cellTableHoveredRow() + " ." + RESOURCES.style().bDeleteCell() + "{visibility: visible}");
        StyleInjector.inject("." + style.cellTableHoveredRowCell() + " ." + RESOURCES.style().bDeleteCell() + "{visibility: visible}");
    }

    public DeleteCell() {
        super(BrowserEvents.CLICK, BrowserEvents.KEYDOWN);
    }

    @Override
    public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
    	if (value != null) {
	        if (!value) {
	            sb.append(template.div(RESOURCES.style().bDeleteCell(),
	                    template.img(finbudget.client.resources.Resources.INSTANCE.delete16x16().getSafeUri(), STRINGS.markToBeDeleted())));
	        }
    	}
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, Boolean value, NativeEvent event,
                               ValueUpdater<Boolean> updater) {
        super.onBrowserEvent(context, parent, value, event, updater);
        if (BrowserEvents.CLICK.equals(event.getType())) {
            onEnterKeyDown(context, parent, value, event, updater);
        }
    }

    @Override
    protected void onEnterKeyDown(Context context, Element parent, Boolean value, NativeEvent event,
                                  ValueUpdater<Boolean> updater) {
        if (!value) {
            if (updater != null) {
                updater.update(true);
                setValue(context, parent, true);
            }
        }
    }

}
